﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Nissen_Model;

namespace Nissen_Dao
{
    public class AnswerPaperDao : DaoBase
    {
        public void SaveOrUpdate(IList<AnswerPaper> answerPapers)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                using (ITransaction t1 = session.BeginTransaction())
                {
                    foreach (var answerPaper in answerPapers)
                    {
                        session.SaveOrUpdate(answerPaper);
                    }
                    t1.Commit();
                }
            }
        }

        public IList<AnswerPaper> GetAnswerPapersSubjectCourseId(string subjectCourseId)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                ICriteria crt = session.CreateCriteria<AnswerPaper>();
                crt.Add(Restrictions.Eq("SubjectCourseID", subjectCourseId));
                return crt.List<AnswerPaper>();
            }
        }

        public IList<AnswerPaper> GetAnswerPapers(string subjectCourseId, string subjectCoursePage)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                ICriteria crt = session.CreateCriteria<AnswerPaper>();
                crt.Add(Restrictions.Eq("SubjectCourseID", subjectCourseId));
                crt.Add(Restrictions.Eq("SubjectCoursePage", subjectCoursePage));
                return crt.List<AnswerPaper>();
            }
        }


        public void Delete(AnswerPaper answerPaper)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                using (ITransaction t1 = session.BeginTransaction())
                {
                    session.Delete(answerPaper);
                    t1.Commit();
                }
            }
        }
    }
}
